package com.xone.xapp;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class ContextUtils {
	
	/*
	 * getting screen width
	 */
	public static int getScreenWidth(Context context) {
		int columnWidth;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		final Point point = new Point();
		point.x = display.getWidth();
		columnWidth = point.x;
		return columnWidth;
	}
	
	public static void confirm(Context context, String message, DialogInterface.OnClickListener ok) {
		confirm(context, message, ok, null);
	}
	
	public static void confirm(Context context, String message, DialogInterface.OnClickListener ok, DialogInterface.OnClickListener cancel) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle("消息确认");
		builder.setMessage(message);
		if (null == ok) {
			ok = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			};
		}
		if (null == cancel) {
			cancel = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			};
		}
		builder.setPositiveButton(android.R.string.ok, ok);
		builder.setNegativeButton(android.R.string.cancel, cancel);
		builder.create().show();
	}
	
	public static int getResourceId(Context context, String pVariableName, String pResourcename) 
	{
	    try {
	        return context.getResources().getIdentifier(pVariableName, pResourcename, context.getPackageName());
	    } catch (Exception e) {
	        e.printStackTrace();
	        return R.drawable.ic_launcher;
	    } 
	}

	public static String getSoftwareVersion(Context context) {
		try {
			PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return packageInfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean isOnline(Context context) {
		return isOnline(context, true);
	}
	
	public static boolean isOnline(Context context, boolean notice) {
		boolean resutl = checkNetworkConnect(context);
		if (notice && !resutl) {
			Toast.makeText(context, "网络暂时无法连接", Toast.LENGTH_SHORT).show();
		}
		return resutl;
	}
	
	/**
	 * 检测服务是否在运行
	 * @param context
	 * @param serviceName
	 * @return
	 */
	public final static boolean isServiceRunning(Context context, String serviceName) {
	    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if (serviceName.equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
	
	public static boolean checkNetworkConnect(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		return info != null && info.isConnectedOrConnecting();
	}
	
	/**
	 * 检测网络情况
	 * @return
	 */
	protected boolean isNetworkOk(Context context) {
		// 获得网络连接服务
		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// State state = connManager.getActiveNetworkInfo().getState();
		// 获取WIFI网络连接状态
		State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		// 判断是否正在使用WIFI网络
		if (State.CONNECTED == state) {
			return true;
		}
		// 获取GPRS网络连接状态
		state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		// 判断是否正在使用GPRS网络
		if (State.CONNECTED == state) {
			return true;
		}
		return false;
	}
	
//	/**
//	 * 检测服务是否在运行
//	 * @param context
//	 * @param serviceName
//	 * @return
//	 */
//	public final static boolean isServiceRunning(Context context, String serviceName) {
//	    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
//	        if (serviceName.equals(service.service.getClassName())) {
//	            return true;
//	        }
//	    }
//	    return false;
//	}
	
}
